package com.splant.android.smart.SystemDefinedLevel.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2016/7/13.
 */
public class System_Util {

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static String getVersion(Context context) {
        String version = "";
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            version = info.versionName;

        } catch (Exception e) {
            e.printStackTrace();

        }
        return version;
    }

    public static int getVersionCode(Context context) {
        int versionCode = -1;
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);

            versionCode = info.versionCode;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public final static String LANGUAGECONFIG_NAME = "languageConfig";
    public final static String LANGUAGECONFIG_LANGUAGE = "language";
    public final static String LANGUAGECONFIG_ISAUTO = "isAuto";

    public static Locale getUsedLocale(Context context) {

        SharedPreferences spf_langeage = context.getSharedPreferences(LANGUAGECONFIG_NAME, Activity.MODE_PRIVATE);
        String language = spf_langeage.getString(LANGUAGECONFIG_LANGUAGE, Locale.ENGLISH.getLanguage());
        boolean isAuto = spf_langeage.getBoolean(LANGUAGECONFIG_ISAUTO, true);

        Locale locale = null;
        if (isAuto)
        {
            locale = Locale.getDefault();
            language = locale.getLanguage();
            if (!language.equals(Locale.SIMPLIFIED_CHINESE.getLanguage())) {
                locale = Locale.ENGLISH;
            }
        }
        else {

            if (language.equals(Locale.SIMPLIFIED_CHINESE.getLanguage()))
                locale = Locale.SIMPLIFIED_CHINESE;
            else
                locale = Locale.ENGLISH;
        }

        return locale;
    }

    public static String getUsedLanguage(Context context)
    {
        Locale locale=getUsedLocale(context);

        return locale.getLanguage();
    }

    public static void configLanguage(Context context) {
        configLanguage(context, getUsedLocale(context));
    }

    public static void configLanguage(Context context, String language) {
        Locale locale = new Locale(language);
        configLanguage(context, locale);

    }

    public static void configLanguage(Context context, Locale locale) {

        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        // 应用用户选择语言
        config.locale = locale;
        resources.updateConfiguration(config, dm);

    }

    public final static String CHARSETNAME_GBK = "GBK";
    public final static String CHARSETNAME_UTF16 = "utf-16";
    public final static String CHARSETNAME_UTF8 = "utf-8";

    public static String getCharsetName(Context context) {

        Locale locale = getUsedLocale(context);
        String charsetName = CHARSETNAME_UTF16;
        String language = locale.getLanguage();

        if (language.equals(Locale.SIMPLIFIED_CHINESE.getLanguage())) {
            charsetName = CHARSETNAME_GBK;
        } else {
            charsetName = CHARSETNAME_UTF8;
        }
        return charsetName;
    }

    public static String getFromAssets(String fileName, Context context, String charsetName) {
        String result = "";
        try
        {
            InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName), Charset.forName(charsetName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null)
                result += line;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getSystemCurrentTime(String format) {
        SimpleDateFormat dateFormater = new SimpleDateFormat(format);
        Date date = new Date();
        return dateFormater.format(date);
    }

    public static void setEditGetFocus(EditText editText)
    {
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

//        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
    public static void showSoftInputFromWindow(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();

        InputMethodManager imm = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText,0);
    }

    public static void hideInput(Context context,View view){
        InputMethodManager inputMethodManager =
                (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
    }



    public static long get24Time() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    public static void openUrlBrowser(String url,Context context)
    {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        context.startActivity(intent);
    }


    public static boolean ExistSDCard() {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            return true;
        } else
            return false;
    }

    public static long getSDFreeSize(){
        //取得SD卡文件路径
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        //获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSize();
        //空闲的数据块的数量
        long freeBlocks = sf.getAvailableBlocks();
        //返回SD卡空闲大小
        //return freeBlocks * blockSize;  //单位Byte
        //return (freeBlocks * blockSize)/1024;   //单位KB
        return (freeBlocks * blockSize)/1024 /1024; //单位MB
    }


}
